﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cayley
{
    public partial class Form1 : Form
    {
        Color pen = Color.Black;
        public Form1()
        {
            InitializeComponent();
            graphics = panel1.CreateGraphics();
        }
        Graphics graphics;
        double th1 = 30 * Math.PI / 180,
            th2 = 20 * Math.PI / 180,
            per1 = 0.6,
            per2 = 0.7;

        private void ColorButton_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                pen = colorDialog1.Color;
            }
        }

        void DrawCayley(int n,double x0,double y0,double leng,double th)
        {
            if (n == 0) return;
            double x1 = x0 + leng * Math.Cos(th),
                y1 = y0 + leng * Math.Sin(th);
            drawLine(x0, y0, x1, y1);
            DrawCayley(n - 1, x1, y1, per1 * leng, th + th1);
            DrawCayley(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0,double y0,double x1,double y1)
        {
            graphics.DrawLine(new Pen(pen), (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            graphics.Clear(BackColor);
            th1 = (double)numericUpDown5.Value;
            th2 = (double)numericUpDown6.Value;
            per1 = (double)numericUpDown3.Value;
            per2 = (double)numericUpDown4.Value;
            DrawCayley((int)numericUpDown1.Value, 200, 310, (double)numericUpDown2.Value, -Math.PI / 2);
        }
    }
}
